package com.srd.phone.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.activity.BaseActivity;
import com.srd.phone.mvp.interfaces.IBaseView;
import com.srd.phone.mvp.presenters.BasePresenter;

public class BaseFragment extends Fragment implements IBaseView {

    public BaseActivity mContext;
    public RequestQueue requestQueue;
    public BasePresenter presenter;
    private String[] urlArrs = new String[]{};

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = (BaseActivity) getActivity();
        requestQueue = ((BaseActivity) getActivity()).requestQueue;
    }

    /*
     *如果有网络请求，调用此方法来加载界面,目的是为了如果没有网络，切换界面用的，
     */

    public View getParentView(LayoutInflater inflater, ViewGroup container, int layoutID) {
        View root = inflater.inflate(R.layout.fragment_base_layout, container, false);
        FrameLayout view = (FrameLayout) root.findViewById(R.id.fl_fragment_parent);
        view.addView(inflater.inflate(layoutID, view, false), 0);
        return view;
    }

    public void replaceFragment(int id, Fragment fragment) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(id, fragment);
        ft.commit();
    }


    public void gotoActivity(Class<?> cls) {
        startActivity(new Intent(getActivity(), cls));
    }

    //当前是否退出fragment
    public boolean isExit() {
        return getActivity().isFinishing() || isAdded() == false;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void returnData(String tag, String data) {

    }

    @Override
    public void onError(String tag, VolleyError error) {

    }

    /**
     * 取消网络请求
     */
    public void cancelRequest(BasePresenter presenter, String... arr) {
        if (presenter != null && arr != null) {
            for (String url : arr) {
                presenter.cancelRequest(url);
            }
        }
    }

    /**
     * 释放P层的view，并且回收数据
     */
    public void recycle(BasePresenter presenter, String... arr) {
        cancelRequest(presenter, arr);
        View view = getView();
        view = null;
    }

    /**
     * 设置地址
     *
     * @param arrs
     */
    public void setUrlArrs(String... arrs) {
        if (arrs != null && arrs.length > 0) {
            urlArrs = arrs;
        }
    }

    @Override
    public void onDestroyView() {
        cancelRequest(presenter, urlArrs);
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        recycle(presenter, urlArrs);
    }
}
